const {
  controller
} = require('./pages/controllers/Controller');

const Controller = require('/pages/controllers/Controller.js').controller;
App({
  onLaunch: function (option) {
    console.log("获取usertoken");
    //获取usertoken
    wx.clearStorageSync("usertoken")
    let usertoken = wx.getStorageSync('usertoken') || '';
    //优先判断usertoken
    if (usertoken) {
      // this.GetLogin();
      //延时判断微信服务器Session状态
      setTimeout(() => {
        wx.checkSession({
          success: () => {
            //session_key 未过期，并且在本生命周期一直有效
          },
          fail: () => {
            // session_key 已经失效，需要重新执行登录流程
            // this.GetLogin(false);
          }
        });
      }, 1500);
    } else {
      //无usertoken，直接登录
      this.GetLogin();
    }

    //异步更新 + 强制更新

    // wx.getUpdateManager 在 1.9.90 才可用，请注意兼容
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      // console.log(res.hasUpdate)
      if (res.hasUpdate) {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否马上重启小程序？',
          success: function (res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          }
        });
      }
    });

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否马上重启小程序？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      });
    })

    updateManager.onUpdateFailed(function () {
      wx.showModal({
        title: '已有新版本',
        content: '新版本已经上线了，请删除当前小程序，重新搜索打开',
      })
    });

  },
  onShow() {
    this.statusAndNavHeight()
  },
  // 获取导航高度
  statusAndNavHeight() {
    // 状态栏高度
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight
    this.globalData.statusBarHeight = statusBarHeight
    // 获取微信胶囊的位置信息
    const menuButtonObject = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度(标题栏高度) = 胶囊高度 + (顶部距离 - 状态栏高度) * 2
    const navBarHeight = menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
    console.log('statusBarHeight',menuButtonObject.height,menuButtonObject.top,menuButtonObject.bottom,statusBarHeight);

    //导航高度
    this.globalData.navHeight = navBarHeight + statusBarHeight + 5;

    // +5 获取数值有偏差，可用wx.createSelectorQuery()获取自定义导航栏底部的坐标即目标高度
  },

  ToLogin(msg) {
    var that = this;
    if (that.globalData.islogin) {
      that.globalData.islogin = false
      wx.login({
        success: res => {
          Controller.appletLogin({
            code: res.code
          }).then(result => {
            console.log(result)
            if (result.data.sessionKey) {
              this.globalData.sessionKey = result.data.sessionKey
            }
            if (result.data.sessionId) {
              wx.setStorageSync('usertoken', result.data.sessionId)
              if (this.usertokenReadyCallback) {
                this.usertokenReadyCallback(result);
              }
            } else {
              console.log("弹框")
              this.ToLoginlink()
            }
            this.globalData.openId = result.data.openId
            console.log(this.globalData.openId)

          }).catch(res => {
            this.ToLoginlink()
          })

        }
      });

    }
  },
  ToLoginlink() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否授权登录?',
      success(res) {
        that.globalData.islogin = true
        if (res.confirm) {
          wx.navigateTo({
            url: "/pages/Login/Login"
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //登录请求
  GetLogin(flag) {

    if (flag == undefined || flag) {
      wx.showLoading({
        title: '登录中...',
        mask: true
      });
    }
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        Controller.appletLogin({
          code: res.code
        }).then(result => {
          console.log(result.data)
          if (result.data.sessionKey) {
            this.globalData.sessionKey = result.data.sessionKey
          }
          if (result.data.sessionId) {
            console.log("已注册")
            wx.setStorageSync('usertoken', result.data.sessionId)
            wx.setStorageSync('accountId', result.data.id)
            if (this.usertokenReadyCallback) {
              this.usertokenReadyCallback(result);
            }
          } else {
            console.log("未注册")
            if (this.usertokenReadyCallback) {
              this.usertokenReadyCallback(result);
            }
            this.ToLoginlink()
          }
          // if(result.data.sessionId){
          //   wx.setStorageSync('usertoken', result.data.sessionId)
          // }
          this.globalData.openId = result.data.openId

          if (flag == undefined || flag) {
            wx.hideLoading();
          }
        }).catch(res => {
          this.ToLoginlink()
          wx.hideLoading();
        })

      }
    });


  },






  //公共变量
  globalData: {
    islogin: true,
    IMG_URL: 'http://111.230.226.223/beauty-industry',
    mobile: '',
    shopId: '',
    myshopinfo: '',
    sessionKey: '',
    openId: '',
    globalStype: 'pinStyle', //1粉色 2黄色
    tabbar: [{
        "text": "店铺首页",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_home.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210511/841732293570592768.png",
      },
      {
        "text": "商城",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_store.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210511/841732415578701824.png",
      },
      {
        "text": "商圈首页",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210323/823862499639033856.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210511/841732516556570624.png",
      },
      {
        "text": "我的会员店",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877483655335772160.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877483669462188032.png",
      },
      {
        "text": "我的",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_my.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210511/841732798690623488.png",
      }
    ],
    tabbar2: [{
        "text": "店铺首页",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877484387837411328.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210929/892789247587647488.png",
      },
      // {
      //   "text": "商城",
      //   "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_store.png",
      //   "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210511/841732415578701824.png",
      // },
      // {
      //   "text": "商圈首页",
      //   "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877484758932652032.png",
      //   "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877484776972353536.png",
      // },
      {
        "text": "我的会员店",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877483655335772160.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210929/892789000522170368.png",
      },
      {
        "text": "店铺好友",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211119/911295286423322624.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211119/911295271944585216.png",
      },
      {
        "text": "我的",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877485038503985152.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210929/892789122647719936.png",
      }
    ],
    contentList: [],
    imguploadurl: 'https://www.xlshhr.com/beauty-industry',
    coupon: null,
    isuseCoupon: 1, //1用优惠券 2不用优惠券
    vipDesc: "",
    tabbar3: [{
        "text": "商圈首页",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877482584869699584.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210929/892788769587986432.png",
      },
      {
        "text": "我的会员店",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877483655335772160.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210929/892789000522170368.png",
      },
      {
        "text": "个人中心",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210818/877483885384957952.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210929/892789122647719936.png",
      },
    ],
    navHeight: '',
    statusBarHeight:''
  },
  //拨打电话
  calling: function (e) {
    console.log(e)
    wx.makePhoneCall({
      phoneNumber: e || this.globalData.mobile,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  }

})